﻿/* Nibiru Studio Engine Version [1062] */
#include "SceneTarget.h"
#include "Apis/NEngineApi.h"
#include "XR/XRApi.h"
#include "EyeManager.h"

ENGINE_NAMESPACE_BEGIN

IMPLEMENT_CLASS(SceneTarget)
IMPLEMENT_RTTI_CUSTOM(SceneTarget, NComponent)

//ENUM_BEGIN(LabelType, LabelType)
// add your enum 
// Display Name, Enum Type Name
// ENUM(Left, LEFT)
//ENUM_END


REFLECTION_BEGIN(SceneTarget)
// add your variable reflection property 
//Enum Class Name, Display Name In Editor, Variable Name In Class, flags default is 0
//ENUM_PROPERTY(LabelType, Type, m_LabelType, 0)
//PROPERTY(Bold, m_IsBold, 0)
//PROPERTY(Rotate, m_EnableRotate, 0)
//FUNCTION(OnButtonClick, SceneTarget, OnClick)

REFLECTION_END


void SceneTarget::Start()
{
    XRApi::SetGazeVisible(false);
    //
    m_pEyeManager = EyeManager::instance();
    //
    m_gazePoint = NActorManager::GetActor("img_gaze_point");
    m_textMessage = NActorManager::GetActor("text_message")->GetComponent<NLabel>();
}

void SceneTarget::Update()
{
    if (NInput::GetKeyDown(KeyboardKeys::Android_DpadLeft) || NInput::GetKeyDown(KeyboardKeys::Android_Back))
    {
        NLevelManager::LoadLevel("scene_calib.nscene");
    }
    updateGazePoint();
}

void SceneTarget::OnDestroy()
{
	// When the component deleted
}

void SceneTarget::PostEditChangeProperty(std::string& propertyName)
{
	// Variable Property changed in Editor
}

void SceneTarget::Serialize(Archive& ar)
{
	Super::Serialize(ar);
	// SERIALIZE(m_EnableRotate);
}

void SceneTarget::LateDeserialize(Archive& ar)
{
	// After all Serialize are executed
	Super::LateDeserialize(ar);
}

void SceneTarget::updateGazePoint()
{
    EyeDataBasic eyeDataBasic;
    ServiceResult serviceResult = m_pEyeManager->getEyeData(eyeDataBasic);
    if (serviceResult.code != 0)
    {
        NDebug::Log("%s getEyeData error, %s", EYE_TAG, serviceResult.toString().c_str());
        if (m_gazePoint->IsActiveSelf())
        {
            m_gazePoint->SetActive(false);
        }
        return;
    }
    if (eyeDataBasic.gazeValid == 0)
    {
        if (m_gazePoint->IsActiveSelf())
        {
            m_gazePoint->SetActive(false);
        }
        return;
    }
    if (!m_gazePoint->IsActiveSelf())
    {
        m_gazePoint->SetActive(true);
    }
    if (m_textMessage->GetText() != "")
    {
        m_textMessage->SetText("");
    }
    m_gazePoint->SetLocalPosition(Vector3(eyeDataBasic.gazeX, eyeDataBasic.gazeY, 0));
}

ENGINE_NAMESPACE_END
